<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">	
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="./../../assets/css/combined.css">
	<script src="http://www.google.com/jsapi" type="text/javascript"></script>
	<script type="text/javascript">
		var path = './../../';
	</script>
	<script src="./../../assets/js/combined.js"></script>
	<title>Template Controller - General - FuelPHP Documentation</title>
</head>
<body>
	<div id="container">
		<header id="header">
			<div class="table">
				<h1>
					<strong>FuelPHP, a PHP 5.3 Framework</strong>
					Documentation
				</h1>
				
				<form id="google_search">
					<p>
						<span id="search_clear">&nbsp;</span>
						<input type="submit" name="search_submit" id="search_submit" value="search" />
						<input type="text" value="" id="search_input" name="search_input" />						
					</p>
				</form>
			</div>
			<nav>
				
				<div class="clear"></div>
			</nav>
			<a href="#" id="toc_handle">table of contents</a>
			<div class="clear"></div>
		</header>
		
		<div id="cse">
			<div id="cse_point"></div>
			<div id="cse_content"></div>
		</div>
		
		<div id="main">
		
			<h2>Template Controller</h2>
	
			<h3>What is a template controller?</h3>
	
			<p>A Template Controller is an extension of the Base Controller which has template support built in using some pre-defined before() and after() methods. Basically it can be used to wrap
			your view in a layout with a header, footer, sidebar, etc.</p>
	
			<h3>Using the template controller</h3>
	
			<p>Like all Controllers you create a class in the <kbd>fuel/app/classes/controller</kbd> directory. They need to extend the <kbd>Controller_Template</kbd> class and are prefixed by default by "Controller_". Below is an example of the controller "example": </p>
	
			<p class="note"><strong>Please note:</strong> by default, all methods of a class that extends Controller_Template will use the template.
			<br />However, it is possible to <a href="#omit_methods">omit methods</a> from the template.</p>
			
	<pre><code>class Controller_Example extends Controller_Template
{
	
		public function action_index()
		{
	        $data = array();
			$this->template->title = 'Example Page';
			$this->template->content = View::forge('test/index', $data);
		}
	}</code></pre>
	
			<h3>Using the template controller with before()</h3>
			
			<p class="note"><strong>Please note:</strong> if you have a before() method in your template controller extension you
				<strong>must</strong> add <kbd>parent::before();</kbd> to that method or the <kbd>$this->template</kbd> will
				not be available. <strong>Make before() compatible</strong> with that of the Controller_Template: use <kbd>before($data = null)</kbd> instead of just before().</p>
	
			
	<pre><code>class Controller_Example extends Controller_Template
{
	
		/**
		 * Make before() compatible with Controller_Template by adding $data = null as a parameter
		 */
		public function before($data = null)
		{
			parent::before(); // Without this line, templating won't work!
			
			// do stuff
		}
	
		public function action_index()
		{
	        $data = array();
			$this->template->title = 'Example Page';
			$this->template->content = View::forge('test/index', $data);
		}
	}</code></pre>
	
			<h3>Example template</h3>
	
			<p>
				The template file is a great place to call up your JS, CSS, etc, structure your HTML and call view partials. It allows you to give your output
				structure. It is just a view file, by default the Template Controller will look here: <kbd>fuel/app/views/template.php</kbd>.
			</p>
	
	<pre><code>&lt;!DOCTYPE html>
	&lt;html>
	&lt;head>
		&lt;meta charset="utf-8">
		&lt;title>&lt;?php echo $title; ?>&lt;/title>
	
		&lt;?php echo Asset::css('main.css'); ?>
	&lt;/head>
	&lt;body>
		&lt;div id="wrapper">
			&lt;h1>&lt;?php echo $title; ?>&lt;/h1>
	
			&lt;div id="content">
				&lt;?php echo $content; ?>
			&lt;/div>
		&lt;/div>
	&lt;/body>
	&lt;/html></code></pre>
	
	        <h3>Changing the default template file</h3>
	
	        <p>
	            You can easily change the default <kbd>APPPATH/views/template.php</kbd> file to something different.<br/>
	            You must set the public variable <kbd>$template</kbd> (note: you don't need .php extension here) to something different, example:
	        </p>
	
	<pre><code>class Controller_Example extends Controller_Template
{
	
	    public $template = 'template_admin';
	
	    public function action_index()
	    {
	        $this->template->title = 'Example Page';
	        $this->template->content = View::forge('test/index', $data);
	    }
	}</code></pre>
	
		<h3 id="omit_methods">Omit methods from the Template Controller</h3>
		
		<p>
			By default, all methods of a class that extends Controller_Template will use the template.<br />
			If you want to omit methods from the template, you will have to set <kbd>$this->auto_render = false</kbd> in before()
		</p>
		
		<p class="note">
			<strong>Please note:</strong> <kbd>$this->auto_render = false</kbd> must come <strong>after</strong> <kbd>parent::before()</kbd>.
		</p>
		
		<p>
			If you want to <strong>use the template</strong> layout, you will have to write <kbd>$this->response->body = $this->template;</kbd>
			<br />If you <strong>don't</strong> want to <strong>use the template</strong> layout, write <kbd>$this->response->body = View::forge('test/index', $data);</kbd>
		</p>
	
		<pre><code>class Controller_Example extends Controller_Template
{
	
	    public $template = 'template_admin';
	
		public function before($data = null)
		{
			parent::before(); // Without this line, templating won't work!
			
			$this->auto_render = false; // Do not autorender templates	
		}
		
		public function action_index()
	    {
	        $this->template->title = 'Example Page';
	        $this->template->content = View::forge('test/index', $data);
			
			$this->response->body = $this->template; // show content in template
	    }
		
		public function action_example()
	    {
			$data['title']	= "Example Page";
			$data['content'] = "Don't show me in the template";
	
	        $this->response->body = View::forge('index/test', $data); // show content without template
	    }
	}</code></pre>
	
		</div>

		<footer>
			<p>
				&copy; FuelPHP Development Team 2010-2011 - <a href="http://fuelphp.com">FuelPHP</a> is released under the MIT license.
			</p>
		</footer>
	</div>
</body>
</html>